#include <stdio.h>
#include <string.h>
#include "seqlist.h"

void initSeqList(SeqList *list) {
    list->size = 0;
}

void registerUser(SeqList *list, const char *username, const char *password) {
    addUser(list, username, password);
}

void loginUser(SeqList *list, const char *username, const char *password) {
    for (int i = 0; i < list->size; i++) {
        if (strcmp(list->users[i].username, username) == 0 && strcmp(list->users[i].password, password) == 0) {
            printf("登陆成功: %s\n", username);
            return;
        }
    }
    printf("登录失败: %s\n", username);
}

void addUser(SeqList *list, const char *username, const char *password) {
    if (list->size < MAX_SIZE) {
        strcpy(list->users[list->size].username, username);
        strcpy(list->users[list->size].password, password);
        list->size++;
    } else {
        printf("表已满  不能再创建.\n");
    }
}

void deleteUserByName(SeqList *list, const char *username) {
    for (int i = 0; i < list->size; i++) {
        if (strcmp(list->users[i].username, username) == 0) {
            printf("删除的用户: %s\n", username);
            memset(&list->users[i], 0, sizeof(User));
            return;
        }
    }
    printf("没找到该用户: %s\n", username);
}

void deleteLastUser(SeqList *list) {
    if (list->size > 0) {
        printf("删除的最后一个用户: %s\n", list->users[list->size - 1].username);
        memset(&list->users[list->size - 1], 0, sizeof(User));
        list->size--;
    } else {
        printf("表空  不能删除.\n");
    }
}

void modifyUserPassword(SeqList *list, const char *username, const char *newPassword) {
    for (int i = 0; i < list->size; i++) {
        if (strcmp(list->users[i].username, username) == 0) {
            strcpy(list->users[i].password, newPassword);
            printf("密码已为用户更新: %s\n", username);
            return;
        }
    }
    printf("用户未找到: %s\n", username);
}

void findUserByName(SeqList *list, const char *username) {
    for (int i = 0; i < list->size; i++) {
        if (strcmp(list->users[i].username, username) == 0) {
            printf("找到用户名: %s, 密码d: %s\n", list->users[i].username, list->users[i].password);
            return;
        }
    }
    printf("未发现该用户: %s\n", username);
}

void printAllUsers(SeqList *list) {
    for (int i = 0; i < list->size; i++) {
        printf("用户: %s, 密码: %s\n", list->users[i].username, list->users[i].password);
    }
}